package com.weikuang.fareast.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.weikuang.fareast.R;
import com.weikuang.fareast.bean.AddGoodsBean;
import com.weikuang.fareast.bean.ChangeHkBean;
import com.weikuang.fareast.bean.QueryInAndOutInfoBean;

import cn.dlc.commonlibrary.ui.adapter.BaseRecyclerAdapter;


/**
 * 查询
 */
public class ChangHkAdapter extends BaseRecyclerAdapter<ChangeHkBean> {

    private OnRecyclerViewItemClickListener mOnItemClickListener;
    @Override
    public int getItemLayoutId(int viewType) {
        return R.layout.item_change_hk;
    }

    public ChangHkAdapter() {

    }

    @Override
    public void onBindViewHolder(CommonHolder holder, int position) {
        ChangeHkBean bean = getItem(position);

        holder.getText(R.id.name).setText(bean.productName);
        holder.getText(R.id.Specification).setText(bean.attribute);
        holder.getText(R.id.productType).setText(bean.productTypeName);


        EditText mExceptionEditText = holder.getView(R.id.ed_num);

        if (mExceptionEditText.getTag(R.id.ed_num) instanceof TextWatcher){
            mExceptionEditText.removeTextChangedListener(((TextWatcher)mExceptionEditText.getTag(R.id.ed_num)));
        }
        //移除了TextWatcher事件后设置item对应的文本
        if (bean.nums == 0) {
            mExceptionEditText.setText("0");
        }else {
            mExceptionEditText.setText(bean.nums + "");
        }
        mExceptionEditText.setSelection(mExceptionEditText.getText().length());
        TextWatcher textWatcher_backs=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.equals("0")){

                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bean.nums= Integer.valueOf(s.toString());
                }
            }
        };
        //设置tag为TextWatcher
        mExceptionEditText.addTextChangedListener(textWatcher_backs);
        mExceptionEditText.setTag(R.id.ed_num,textWatcher_backs);
        Button btn_sure = holder.getView(R.id.btn_sure);
        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(view,bean,position);
                }
            }
        });

    }

    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, ChangeHkBean data, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
